package cn.iocoder.yudao.module.dify.service.taskprocessingrecords;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.module.dify.dal.dataobject.fileprocessing.FileProcessingDO;
import cn.iocoder.yudao.module.dify.service.fileprocessing.FileProcessingService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.dify.controller.admin.taskprocessingrecords.vo.*;
import cn.iocoder.yudao.module.dify.dal.dataobject.taskprocessingrecords.TaskProcessingRecordsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.dify.dal.mysql.taskprocessingrecords.TaskProcessingRecordsMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dify.enums.ErrorCodeConstants.*;

/**
 * 用户调用dify任务工作流处理记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class TaskProcessingRecordsServiceImpl implements TaskProcessingRecordsService {

    @Resource
    private TaskProcessingRecordsMapper taskProcessingRecordsMapper;

    @Resource
    private FileProcessingService fileProcessingService;

    @Override
    public Long createTaskProcessingRecords(TaskProcessingRecordsSaveReqVO createReqVO) {
        // 插入

        List<FileProcessingDO> fileMsgByFileId = fileProcessingService.getFileMsgByFileId(createReqVO.getFileIdList());
        if (CollectionUtil.isNotEmpty(fileMsgByFileId)){
            List<String> fileName = fileMsgByFileId.stream().map(FileProcessingDO::getName).collect(Collectors.toList());
            createReqVO.setOriginalName(fileName);
        }
        TaskProcessingRecordsDO taskProcessingRecords = BeanUtils.toBean(createReqVO, TaskProcessingRecordsDO.class);
        taskProcessingRecordsMapper.insert(taskProcessingRecords);
        // 返回
        return taskProcessingRecords.getId();
    }

    @Override
    public void updateTaskProcessingRecords(TaskProcessingRecordsSaveReqVO updateReqVO) {
        // 校验存在
        validateTaskProcessingRecordsExists(updateReqVO.getId());
        // 更新
        TaskProcessingRecordsDO updateObj = BeanUtils.toBean(updateReqVO, TaskProcessingRecordsDO.class);
        taskProcessingRecordsMapper.updateById(updateObj);
    }

    @Override
    public void deleteTaskProcessingRecords(Long id) {
        // 校验存在
        validateTaskProcessingRecordsExists(id);
        // 删除
        taskProcessingRecordsMapper.deleteById(id);
    }

    private void validateTaskProcessingRecordsExists(Long id) {
        if (taskProcessingRecordsMapper.selectById(id) == null) {
            throw exception(TASK_PROCESSING_RECORDS_NOT_EXISTS);
        }
    }

    @Override
    public TaskProcessingRecordsDO getTaskProcessingRecords(Long id) {
        return taskProcessingRecordsMapper.selectById(id);
    }

    @Override
    public PageResult<TaskProcessingRecordsDO> getTaskProcessingRecordsPage(TaskProcessingRecordsPageReqVO pageReqVO) {
        return taskProcessingRecordsMapper.selectPage(pageReqVO);
    }

}